<template>
  <div class="modal">
    <el-dialog
      v-model="dialogVisible"
      :title="
        isNewRef ? modalConfig.header.newTitle : modalConfig.header.editTitle
      "
      width="30%"
      center
    >
      <div class="form">
        <el-form :modal="formData" label-width="80px" size="large">
          <template v-for="item in modalConfig.formItems" :key="item.prop">
            <template v-if="item.type === 'input'">
              <el-form-item :label="item.label" :prop="item.prop">
                <el-input
                  v-model="formData[item.prop]"
                  :placeholder="item.placeholder"
                ></el-input>
              </el-form-item>
            </template>
            <template v-else-if="item.type === 'custom'">
              <slot :name="item.slotName"></slot>
            </template>
            <template v-else>
              <el-form-item :label="item.label" :prop="item.prop">
                <el-select
                  v-model="formData[item.prop]"
                  :placeholder="item.placeholder"
                  style="width: 100%"
                >
                  <el-option
                    v-for="i in item.options"
                    :key="i.value"
                    :label="i.label"
                    :value="i.label"
                  />
                </el-select> </el-form-item
            ></template>
          </template>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleConfirmClick">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useSystemStore } from '@/stores/main/system/stytem'

interface IRoleProps {
  modalConfig: {
    pageName: string
    header: {
      newTitle: string
      editTitle: string
    }
    formItems: any[]
  }
  otherInfo?: any
}
const props = defineProps<IRoleProps>()

const dialogVisible = ref(false)
const isNewRef = ref(true)

const initialData: any = {}
for (const item of props.modalConfig.formItems) {
  initialData[item.prop] = item.initialData ?? ''
}
const formData = reactive(initialData)
const editData = ref()
// 设置show或者hide
function setModalVisible(hasNew: boolean = true, data?: any) {
  dialogVisible.value = true
  // 判断密码框是否显示
  isNewRef.value = hasNew
  if (!hasNew && data) {
    // 编辑用户回显数据
    for (const key in data) {
      formData[key] = data[key]
    }
    editData.value = data
  } else {
    // 新建用户清空数据
    for (const key in formData) {
      formData[key] = ''
    }

    editData.value = null
  }
}

// 新建用户的store
const systemStore = useSystemStore()

// 确定按钮逻辑
function handleConfirmClick() {
  dialogVisible.value = false

  let infoData = formData
  if (props.otherInfo) {
    infoData = { ...infoData, ...props.otherInfo }
  }
  if (!isNewRef.value && editData.value) {
    // 编辑数据
    systemStore.editPageDataAction(
      props.modalConfig.pageName,
      editData.value.id,
      infoData
    )
  } else {
    //创建新的用户
    systemStore.newPageDataAction(props.modalConfig.pageName, infoData)
  }
}
// 暴露
defineExpose({ setModalVisible })
</script>

<style lang="scss" scoped></style>
